/**
 * creation: 7-mrt-2008
 */
package org.jfonia.connect;

/**
 * Simple iterator over an array of type T.
 * The current value can be obtained by get() or changed by set().
 * Call() moves the iterator to the next element.
 * <p>
 * Note that by default the iterator starts at index -1, i.e. <i>before</i> the start
 * of the array. This means that you have to call() before accessing the first value. 
 * This is similar to a ResultSet-iterator in JDBC. 
 * The iterator never moves beyond the last element. 
 * 
 * @author wijnand.schepens@gmail.com
 *
 */
public class SimpleArrayIterator<T> extends BasicNode implements MutableValue<T>
{
	protected T[] array;
	protected int index;
	
	/**
	 * @param array
	 * @param index
	 */
	public SimpleArrayIterator(T[] array, int index)
	{
		this.array = array;
		this.index = index;
	}

	/**
	 * @param array
	 */
	public SimpleArrayIterator(T[] array)
	{
		this.array = array;
		this.index = -1;
	}

	/**
	 * Move to the next element, if not already at end of the array.
	 * If iterator is moved then listeners are called and the method returns true.
	 * Otherwise it returns false.
	 */
	public boolean call(Object param)
	{
		if (index+1 < array.length)
		{
			index++;
			return super.callThis(this);
		}
		else
			return false;
	}

	public void set(T newValue)
	{
		array[index] = newValue;
	}

	public T get()
	{
		return array[index];
	}

}
